home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Archives / GNU / GNUPLOTsrc.lha / term / multitos.trm < prev    next >
Encoding:
Text File  |  1996-01-22  |  8.3 KB  |  379 lines

  1. /* GNUPLOT - mtos.trm */
  2. /*
  3.  * Copyright (C) 1994   
  4.  *
  5.  * Permission to use, copy, and distribute this software and its
  6.  * documentation for any purpose with or without fee is hereby granted, 
  7.  * provided that the above copyright notice appear in all copies and 
  8.  * that both that copyright notice and this permission notice appear 
  9.  * in supporting documentation.
  10.  *
  11.  * Permission to modify the software is granted, but not the right to
  12.  * distribute the modified code.  Modifications are to be distributed 
  13.  * as patches to released version.
  14.  *  
  15.  * This software  is provided "as is" without express or implied warranty.
  16.  * 
  17.  * This file is included by ../term.c.
  18.  *
  19.  * This terminal driver supports:
  20.  *   mtos : ATARI Systems MiNT/MULTITOS/MAGIC with external client
  21.  *
  22.  * AUTHOR
  23.  *  Dirk Stadler (email: dirk_stadler@n.maus.de, dirk@lstm.uni-erlangen.de)
  24.  * 
  25.  * send your comments or suggestions to (info-gnuplot@dartmouth.edu).
  26.  * 
  27.  */
  28.  
  29. #ifndef GOT_DRIVER_H
  30. #include "driver.h"
  31. #endif
  32.  
  33. #ifdef TERM_REGISTER
  34. register_term(mtos)
  35. #endif           
  36.  
  37. #ifdef TERM_PROTO
  38.  
  39. /* function-prototypes */
  40. TERM_PUBLIC void MTOS_options(void);
  41. TERM_PUBLIC void MTOS_init(void);
  42. TERM_PUBLIC void MTOS_reset(void);
  43. TERM_PUBLIC void MTOS_graphics(void);
  44. TERM_PUBLIC void MTOS_text(void);
  45. TERM_PUBLIC void MTOS_move(unsigned int x, unsigned int y);
  46. TERM_PUBLIC void MTOS_vector(unsigned int x, unsigned int y);
  47. TERM_PUBLIC void MTOS_linetype(int lt);
  48. TERM_PUBLIC int MTOS_text_angle(int ta);
  49. TERM_PUBLIC void MTOS_put_text(unsigned int x, unsigned int y, char *str);
  50. TERM_PUBLIC int  MTOS_justify_text(enum JUSTIFY mode);
  51. TERM_PUBLIC void MTOS_point(unsigned int x, unsigned int y, int number);
  52.  
  53. /* default to ST high resolution */
  54. #define MTOS_XMAX  640
  55. #define MTOS_YMAX  400
  56. #define MTOS_VCHAR 18
  57. #define MTOS_HCHAR 12
  58. #define MTOS_HTIC  5
  59. #define MTOS_VTIC  MTOS_HTIC
  60.  
  61. #define GOT_MTOS_PROTO
  62. #endif /* TERM_PROTO */
  63.  
  64. #ifndef TERM_PROTO_ONLY
  65. #ifdef TERM_BODY
  66.  
  67. #include <unistd.h>
  68. #include <fcntl.h>
  69. #include <param.h>
  70. #include <process.h>
  71. #include <signal.h>
  72. #include <support.h>
  73. #include <sys/stat.h>
  74. #include <time.h>
  75. #include <errno.h>
  76. #include <aesbind.h>
  77. #include <osbind.h>
  78. #include <mintbind.h>
  79.  
  80. int  MTOS_pause(char *str);
  81. static void MTOS_quit(void);
  82. void MTOS_open_pipe(void);
  83. static void write_pid(void);
  84. static void init_exit(void);
  85.  
  86. /* commands for GPCLIENT */
  87. #define SET_GRAPHICS    'G'
  88. #define SET_TEXT        'E'
  89. #define SET_LINE        'L'
  90. #define SET_ANGLE       'A'
  91. #define SET_JUSTIFY     'J'
  92. #define SET_POINTMODE   'D'
  93. #define GR_MOVE         'M'
  94. #define GR_DRAW         'V'
  95. #define GR_RESET        'R'
  96. #define GR_TEXT         'T'
  97.  
  98. #define PAUSE           'P'
  99. #define PID             'I'
  100. #define QUIT            'Q'
  101.  
  102. /* for creating pipes */
  103. #define PIPE1           "u:\\pipe\\GPLT%d"
  104. #define PIPE2           "u:\\pipe\\gpclient.tmp"
  105. #define NAMESIZE        25
  106. #define SIZE            ((unsigned)sizeof(short))
  107.  
  108. /* default name for client */
  109. #define GPCLIENT1       "gpclient.prg"
  110. #define GPCLIENT2       "gpclient"
  111.  
  112. /* environment */
  113. #define MYENV          "GNUPLOTPATH" 
  114. #define PATH           "PATH"
  115.  
  116. /* some global variables */
  117. static char MTOS_mode = 0;
  118. static int handle = -1;
  119. static int pid = -1;
  120.  
  121. TERM_PUBLIC void MTOS_options()
  122. {
  123.     term_options[0] = '\0';
  124. }
  125.  
  126. TERM_PUBLIC void MTOS_init() 
  127.     char pipe[NAMESIZE];
  128.     char *file, cmd[MAXPATHLEN];
  129.     char const *const ext[] = {"prg", "app", NULL};
  130.  
  131.     if (handle < 0) {
  132.         if (aesid < 0) {
  133.             if ((aesid = appl_init()) < 0)
  134.                 int_error("APPL_INIT failed !", NO_CARET);            
  135.         }
  136.  
  137.         file = findfile(GPCLIENT2,getenv(MYENV),ext);
  138.         if (!file)
  139.             strcpy(cmd,GPCLIENT1);
  140.         else if (file && !strchr(file,'\\') && !strchr(file,'/'))
  141.             strcpy(cmd,file);
  142.         else
  143.             unx2dos(file,cmd);
  144.         if (!shel_find(cmd))
  145.             int_error("Cannot find GPCLIENT !", NO_CARET);
  146.  
  147.         sprintf(&pipe[1],PIPE1,aesid);
  148.  
  149.         if ((handle = open(&pipe[1], O_RDWR | O_CREAT)) < 0) {
  150.             if ((handle = (int)Fcreate(&pipe[1],0)) < 0)
  151.                 int_error("Cannot open PIPE to GPCLIENT !", NO_CARET);
  152.         }
  153.         
  154.         pipe[0] = (char)(strlen(&pipe[1])+1);
  155.  
  156.         if (!shel_write(1,1,100,cmd,pipe)) {
  157.             close(handle);
  158.             handle = -1;
  159.             int_error("Cannot spawn GPCLIENT !", NO_CARET);
  160.         }
  161.            init_exit();
  162.     }
  163.     if (aesid > -1) menu_register(aesid, "  Terminal: mtos");
  164. }
  165.  
  166. TERM_PUBLIC void MTOS_reset() 
  167. {
  168.     short buff;
  169.  
  170.     buff = (short)GR_RESET;
  171.     write(handle, &buff, SIZE); 
  172. }
  173.  
  174. TERM_PUBLIC void MTOS_text() 
  175. {
  176.     short buff;
  177.  
  178.     buff = (short)SET_TEXT;
  179.     if (MTOS_mode != SET_TEXT) 
  180.            write(handle, &buff, SIZE);
  181.     MTOS_mode = SET_TEXT;
  182. }
  183.  
  184. TERM_PUBLIC void MTOS_graphics()
  185.     short buff;
  186.  
  187.     buff = (short)SET_GRAPHICS;
  188.     write(handle, &buff, SIZE);
  189.     MTOS_mode = SET_GRAPHICS ;
  190. }
  191.  
  192. TERM_PUBLIC void MTOS_move(unsigned int x, unsigned int y)
  193.     short x_1, y_1, buff;
  194.  
  195.     x_1 = (short)x;
  196.     y_1 = (short)y;     
  197.     buff = (short)GR_MOVE;    
  198.     write(handle, &buff, SIZE);
  199.     write(handle, &x_1, SIZE);
  200.     write(handle, &y_1, SIZE);
  201. }
  202.  
  203. TERM_PUBLIC void MTOS_vector(unsigned int x, unsigned int y)
  204.     short x_1, y_1, buff;
  205.  
  206.     x_1 = (short)x;
  207.     y_1 = (short)y;    
  208.     buff = (short)GR_DRAW;
  209.     write(handle, &buff, SIZE);
  210.     write(handle, &x_1, SIZE);
  211.     write(handle, &y_1, SIZE);
  212. }
  213.  
  214. TERM_PUBLIC void MTOS_linetype(int lt)
  215.     short lt_1, buff;
  216.  
  217.     lt_1 = (short)lt;
  218.     buff = (short)SET_LINE;
  219.     write(handle, &buff, SIZE);
  220.     write(handle, <_1, SIZE);
  221. }
  222.  
  223. TERM_PUBLIC int MTOS_text_angle(int ta)
  224.     short ta_1, buff;
  225.  
  226.     ta_1 = (short)ta;
  227.     buff = (short)SET_ANGLE;
  228.     write(handle, &buff, SIZE);
  229.     write(handle, &ta_1, SIZE);
  230.     return(TRUE);
  231. }
  232.  
  233. TERM_PUBLIC void MTOS_put_text(unsigned int x, unsigned int y, char *str) 
  234. {
  235.     short x_1, y_1, len, buff;
  236.  
  237.     x_1 = (short)x;
  238.     y_1 = (short)y;
  239.     len = (short)strlen(str) + 1;
  240.     buff = (short)GR_TEXT;    
  241.     write(handle, &buff, SIZE);
  242.     write(handle, &x_1, SIZE);
  243.     write(handle, &y_1, SIZE);
  244.     write(handle, &len, SIZE);
  245.     write(handle, str, (unsigned)len);
  246. }
  247.  
  248. TERM_PUBLIC int MTOS_justify_text(enum JUSTIFY mode)
  249. {
  250.     short j_mode, buff;
  251.  
  252.     j_mode = (short)mode;
  253.     buff = (short)SET_JUSTIFY;    
  254.     write(handle, &buff, SIZE);
  255.     write(handle, &j_mode, SIZE);
  256.     return(TRUE);
  257. }
  258.  
  259. TERM_PUBLIC void MTOS_point(unsigned int x, unsigned int y, int number)
  260. {
  261.     short mode, buff;
  262.  
  263.     buff = (short)SET_POINTMODE;    
  264.     mode = 1;
  265.     write(handle, &buff, SIZE);
  266.     write(handle, &mode, SIZE);    
  267.     do_point(x, y, number); 
  268.     mode = 0;
  269.     write(handle, &buff, SIZE);
  270.     write(handle, &mode, SIZE);
  271. }
  272.  
  273. int MTOS_pause(char *str)
  274. {
  275.     short len, buff;
  276.  
  277.     len = (short)strlen(str) + 1;
  278.     buff = (short)PAUSE;    
  279.     write(handle, &buff, SIZE);
  280.     write(handle, &len, SIZE);
  281.     write(handle, str, (unsigned)len);
  282.     read (handle, &len, SIZE);
  283.     return((int)len);
  284. }
  285.  
  286. static void MTOS_quit()
  287. {
  288.     short buff;
  289.     
  290.     if (pid > -1)
  291.         kill(pid,SIGTERM);
  292.     if (handle > -1) {
  293.         buff = (short)QUIT;
  294.         write(handle, &buff, SIZE);
  295.         close(handle);
  296.     }
  297. }
  298.  
  299. void MTOS_open_pipe()
  300. {
  301.     char pipe[NAMESIZE];
  302.     short len;
  303.     
  304.     if (handle < 0) {
  305.         if ((handle = open(PIPE2, O_RDWR)) < 0)
  306.                 return;
  307.         read(handle, &len, SIZE);
  308.         if (len > 0) {
  309.                 read(handle, pipe, (unsigned)len);
  310.                 close(handle);
  311.                 if ((handle = open(pipe, O_RDWR)) < 0) {
  312.                     fprintf(stderr,"\n\n\33p Can't open Pipe: (%s) Error: (%s) !\33q\n\n",pipe,sys_errlist[errno]);
  313.                     fflush(stderr);
  314.                     return;
  315.                 }
  316.         }
  317.         init_exit();
  318.     }
  319. }
  320.  
  321. static void write_pid()
  322. {
  323.     short buff, mypid, gpclpid;
  324.     
  325.     mypid = (short)getpid();
  326.     buff = (short)PID;
  327.     while (write(handle, &buff, SIZE) <= 0)
  328.         Fselect(100,0L,0L,0L);
  329.     while (read(handle, &gpclpid, SIZE) <= 0)
  330.         Fselect(100,0L,0L,0L);
  331.     write(handle, &mypid, SIZE);
  332.     pid = (int)gpclpid;
  333. }
  334.  
  335. static void init_exit()
  336. {
  337.     Cconout(7);
  338.     write_pid();
  339.     atexit(MTOS_quit);
  340. }
  341.  
  342. #endif /* TERM_BODY */
  343.  
  344. #ifdef TERM_TABLE
  345.  
  346. TERM_TABLE_START(mtos_driver)
  347.     "mtos", "Atari MiNT/MULTITOS/Magic Terminal",
  348.     MTOS_XMAX, MTOS_YMAX, MTOS_VCHAR, MTOS_HCHAR, 
  349.     MTOS_VTIC, MTOS_HTIC, MTOS_options, MTOS_init, MTOS_reset, 
  350.     MTOS_text, null_scale, MTOS_graphics, MTOS_move, MTOS_vector, 
  351.     MTOS_linetype, MTOS_put_text, MTOS_text_angle, 
  352.     MTOS_justify_text, MTOS_point, do_arrow, set_font_null,
  353.     0, TERM_CAN_MULTIPLOT, 0, 0
  354. TERM_TABLE_END(mtos_driver)
  355.  
  356. #undef LAST_TERM
  357. #define LAST_TERM mtos_driver
  358.  
  359. #endif /* TERM_TABLE */
  360.  
  361. #endif /* TERM_PROTO_ONLY */
  362.  
  363. #ifdef TERM_HELP
  364. START_HELP(mtos)
  365. "1 mtos",
  366. "?set terminal mtos",
  367. "?mtos",
  368. " The `mtos` terminal has no options.  It sends data via a pipe to an external",
  369. " program called GPCLIENT. It runs under MiNT, MULTITOS, Magic 3.x and MagicMAC.",
  370. " If you cannot find GPCLIENT, than mail to dirk@lstm.uni-erlangen.de."
  371. END_HELP(mtos)
  372. #endif /* TERM_HELP */
  373.